package com.example.administrator.xxnapp.tucao;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.ImageBean;

import java.util.List;

/**
 * Created by Administrator on 2018/5/2.
 */

public class RVImageAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ImageBean> mDatas;
    private final int ADD_NEW_PHOTO = 1;
    private final int NORMAL_PHOTO = 2;

    @Override
    public int getItemViewType(int position) {
        if (mDatas != null){
            if (mDatas.size()>0 && position==mDatas.size()){
                return ADD_NEW_PHOTO;
            }else {
                return NORMAL_PHOTO;
            }
        }else {
            return NORMAL_PHOTO;
        }
//        return super.getItemViewType(position);
    }

    public RVImageAdapter(Context context, List<ImageBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case NORMAL_PHOTO:
                holder = new RVImageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_image_layout,parent,false));
                break;
            case ADD_NEW_PHOTO:
                holder = new RVAddPhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_add_image_layout,parent,false));
                break;
            default:
                holder = new RVImageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_image_layout,parent,false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RVImageViewHolder){
            ((RVImageViewHolder) holder).bindView(position);
        }else if (holder instanceof RVAddPhotoViewHolder){
            ((RVAddPhotoViewHolder) holder).bindView(position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null?0:mDatas.size()==0?0:mDatas.size()+1;
    }

    private class RVImageViewHolder extends RecyclerView.ViewHolder{
        private ImageView mIV;

        public RVImageViewHolder(View itemView) {
            super(itemView);
            mIV = itemView.findViewById(R.id.item_image_layout_IV);
        }

        void bindView(final int position){
            Glide.with(context).load(mDatas.get(position).getPath()).into(mIV);
            mIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemImageClickListener != null){
                        onItemImageClickListener.onItemImageClick(position);
                    }
                }
            });
        }
    }

    private class RVAddPhotoViewHolder extends RecyclerView.ViewHolder{
        private ImageView mIV;

        public RVAddPhotoViewHolder(View itemView) {
            super(itemView);
            mIV = itemView.findViewById(R.id.item_image_layout_IV);
        }
        void bindView(final int position){
            mIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemImageClickListener != null){
                        onItemImageClickListener.onItemAddClick(position);
                    }
                }
            });
        }
    }

    private OnItemImageClickListener onItemImageClickListener;

    public void setOnItemImageClickListener(OnItemImageClickListener onItemImageClickListener) {
        this.onItemImageClickListener = onItemImageClickListener;
    }

    public interface OnItemImageClickListener{
        void onItemImageClick(int position);
        void onItemAddClick(int position);
    }
}
